import Taro, {useReducer} from '@tarojs/taro'
import {Button, View} from '@tarojs/components'

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return state + 1;
    case 'decrement':
      return state - 1;
    default:
      throw new Error();
  }
}

function Counter2({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount);
  return (
    <View>
      Count: {state}
      <Button onClick={() => dispatch({type: 'increment'})}>+</Button>
      <Button onClick={() => dispatch({type: 'decrement'})}>-</Button>
    </View>
  );
}

export default Counter2
